package cn.moukou.dictator.util;

import cn.moukou.dictator.pojo.Student;
import org.neo4j.ogm.typeconversion.CompositeAttributeConverter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentConverter implements CompositeAttributeConverter<Student> {
    @Override
    public Map<String, ?> toGraphProperties(Student entity) {
        Map<String, Object> properties = new HashMap<>();
        if (entity != null)  {
            properties.put("student_name", entity.getName());
            properties.put("student_age", entity.getAge());
            properties.put("student_sex", entity.getSex());
            properties.put("student_avatar", entity.getAvatar());
            properties.put("student_class_label", entity.getClassLabel());
            properties.put("student_mark_list", entity.getMarkList());
            properties.put("student_rank_list", entity.getRankList());
            properties.put("student_mark_time_list", entity.getMarkTimeList());
            properties.put("student_award_list", entity.getAwardList());
            properties.put("student_activity_list", entity.getActivityList());
        }
        return properties;
    }

    @Override
    public Student toEntityAttribute(Map<String, ?> map) {
        if(!map.get("node_type").toString().equals("student")) {
            return null;
        }
        Student entity = new Student();
        entity.setName((String) map.get("student_name"));
        entity.setAge((String) map.get("student_age"));
        entity.setSex((String) map.get("student_sex"));
        entity.setAvatar((String) map.get("student_avatar"));
        entity.setClassLabel((String) map.get("student_class_label"));
        entity.setMarkList((List<String>) map.get("student_mark_list"));
        entity.setRankList((List<String>) map.get("student_rank_list"));
        entity.setMarkTimeList((List<String>) map.get("student_mark_time_list"));
        entity.setAwardList((List<String>) map.get("student_award_list"));
        entity.setActivityList((List<String>) map.get("student_activity_list"));

        List<String> markList = entity.getMarkList();
        List<String> rankList = entity.getRankList();
        if (markList!=null) {
           entity.setLatestMark(markList.get(markList.size()-1));
        } else {
            entity.setLatestMark("-");
        }
        if(rankList!=null) {
            entity.setLatestRank(rankList.get(rankList.size()-1));
        } else {
            entity.setLatestRank("-/-");
        }
        return entity;

    }
}
